#!/bin/sh

check()
{
  cat << EOF > dummy.c
#include <stdlib.h>
#include <locale.h>
#include <libintl.h>
#include <limits.h>

int main ()
{
  textdomain (NULL);
  return 0;
}
EOF
  gcc dummy.c "$@" > /dev/null 2>&1
}

FLAGS=""
if check || check "-lintl" || check "-lintl" "-liconv"; then
  rm -f "$A_OUT" dummy.c
  $1 $2
  if [ -r "$A_OUT" ] ; then
    rm -rf tmplocale &&
    mkdir tmplocale tmplocale/de_DE tmplocale/de_DE/LC_MESSAGES &&
    { echo 'msgid "foobar"'; echo 'msgstr "bazqux"'; } > tmplocale/de_DE/LC_MESSAGES/gettext2test.po &&
    if (cd ./tmplocale/de_DE/LC_MESSAGES && msgfmt -o gettext2test.mo gettext2test.po > /dev/null 2>&1) then
      exec 4>&2 2> /dev/null
      LANG=de_DE LC_ALL=de_DE LC_MESSAGES=de_DE
      exec 2>&4
      export LANG LC_ALL LC_MESSAGES
      ./"$A_OUT"
    else
      echo "SKIPPED: could not run msgfmt"
    fi
    rm -rf tmplocale
  else
    echo "failed"
  fi
else
  rm -f dummy.c
  echo "SKIPPED: libintl not installed"
fi
